package twoD.hofem.Q;

import java.util.ArrayList;

/**
 * Create the Mesh class 
 * 
 * 
 * @author White Horse
 * 
 */
public class Mesh {
	
	private int nDofs_;
	private int d_;
	private int p_=2;
	private boolean solutionAvailable_ = false;
	private ArrayList<Edge> EdgeList_ = new ArrayList<Edge>();
	private ArrayList<Node> NodeList_ = new ArrayList<Node>();
	private ArrayList<Face> FaceList_ = new ArrayList<Face>();

	
	public Edge addEdge (Edge e){
		EdgeList_.add(e);
		return e;
	}
	
	public Face addFace (Face f){
		FaceList_.add(f);
		return f;
	}
	
	public Node addNode (Node n){
		NodeList_.add(n);
		return n;
	}
	
	public int countEdges(){
		return EdgeList_.size();
	}
	
	public int countFaces(){
		return FaceList_.size();
	}
	
	public int countNodes(){
		return NodeList_.size();
	}
	
	public int countDofs(){
		return nDofs_;
	}
	
	public int getD(){
		return d_;
	}
	
	public Edge getEdge (int idx){
		return EdgeList_.get(idx);
	}
	
	public Face getFace (int idx){
		return FaceList_.get(idx);
	}
	
	public Node getNode (int idx){
		return NodeList_.get(idx);
	}
	
	public int getP(){
		return p_;
	}
	
	/*
	 * Enumenrates DOFs for all nodes, edgesm and faces of Mesh.
	 * 
	 * @return next number of DOFs
	 */
	public void enumerateDofs(){
		nDofs_ = 0 ;
		for (int i = 0 ; i < NodeList_.size() ; i++){
			NodeList_.get(i).setD(d_);
			nDofs_ = NodeList_.get(i).enumerateDofs(nDofs_);
			NodeList_.get(i).setUpTArray();
		}		
		for (int i = 0 ; i < EdgeList_.size() ; i++){
			EdgeList_.get(i).setD(d_);
			EdgeList_.get(i).setP(p_);
			nDofs_ = EdgeList_.get(i).enumerateDofs(nDofs_);
			EdgeList_.get(i).setUpTArray();
		}		
		for (int i = 0 ; i < FaceList_.size() ; i++){
			FaceList_.get(i).setD(d_);
			FaceList_.get(i).setP(p_);
			nDofs_ = FaceList_.get(i).enumerateDofs(nDofs_);
			FaceList_.get(i).setUpTArray();
		}
	}
	
	public void setD (int d){
		d_=d;
	}
	
	public void setP (int p){
		p_=p;
		if (p_<1){
			p_ = 1;
		}
	}
	
	public void setUGlobal (double[] uGlobal){
		// set for nodes
		for (int i = 0 ; i < NodeList_.size() ; i++ ){
			NodeList_.get(i).setUGlobal(uGlobal);
		}
		// set for edges
		for (int i = 0 ; i < EdgeList_.size() ; i++ ){
			EdgeList_.get(i).setUGlobal(uGlobal);
		}
		// set for faces
		for (int i = 0 ; i < FaceList_.size() ; i++ ){
			FaceList_.get(i).setUGlobal(uGlobal);
		}
		// set solutionAvailable to true
		solutionAvailable_ = true;
	}
	
	public boolean solutionAvailable(){
		return solutionAvailable_;
	}

}
